package com.sztx.pay.center.common.util;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.*;

import java.lang.reflect.Type;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sztx.se.common.exception.BusinessException;

public class ObjectUtil {
	private static final Logger LOG = LoggerFactory.getLogger(ObjectUtil.class);
	private static Gson  gson  = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
		   public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
			      return new Date(json.getAsJsonPrimitive().getAsLong()); 
			   } 
			}).create();
	
	public static Map<String, String> transBean2Map(Object obj) {
		if (obj == null) {
			return null;
		}
		
		Map<String, String> map = new HashMap<String, String>();
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
			for (PropertyDescriptor property : propertyDescriptors) {
				String key = property.getName();
				// 过滤class属性
				if (!key.equals("class")) {
					// 得到property对应的getter方法
					Method getter = property.getReadMethod();
					Object value = getter.invoke(obj);					
					if (value == null) {
						map.put(key, null);
					}else {
						map.put(key, value.toString());
					}
				}
			}
		} catch (Exception e) {
			throw new BusinessException("transBean2Map Error ", e);
		}
		return map;
	}
	
	
	/** 
     * 把数组所有元素排序，并按照“参数=参数值”的模式用“&”字符拼接成字符串
     * @param params 需要排序并参与字符拼接的参数组
     * @return 拼接后字符串
     */
    public static String createLinkString(Map<String, String> params, boolean ignoreNull) {

        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);

        String prestr = "";

        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);

            if (ignoreNull && StringUtil.isEmpty(value)) {
				continue;
			}
            
            if (value == null) {
				value = "";
			}
            
            
            if (StringUtil.isBlank(prestr)) {//拼接时，不包括第一个&字符
                prestr = prestr + key + "=" + value;
            } else {
                prestr = prestr + "&" + key + "=" + value;
            }
        }

        return prestr;
    }

	@SuppressWarnings("unchecked")
	public static <T> T copyProperties(Object source,T target){
		long currentTime = System.currentTimeMillis();
		
		String originStr = JSONObject.toJSONString(source);
		T t =  (T)gson.fromJson(originStr,target.getClass());
		
		long endTime = System.currentTimeMillis();
		LOG.debug("copyProperties cost time:"+(endTime-currentTime)+"  ms");
		return t;
	}
	
	
	
}
